<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * sitech
 *
 * @package		sitech
 * @author		SITECH : Dev Team - Sokthea Peng
 * @license		http://sitech.com.kh
 * @link		http://sitech.com.kh
 * @since		Version 0.9.4
 */

// ------------------------------------------------------------------------


/**
 * sitech Media Model
 *
 * @package		sitech
 * @subpackage	Models
 * @category	All Models
 * @author		SITECH : Dev Team - Sokthea Peng
 *
 */

class acc_model extends Base_model 
{
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	public function __construct()
	{
		parent::__construct();

		$this->pk_name	= 	'id';	 
			
	}

	/**
	 * Returns the list
	 *
	 */
	function get($table, $orderby = FALSE, $options = FALSE) // $orderby (Field Name) //$options (ASC or DESC)
	{
		if($orderby == FALSE)
			$this->db->order_by($this->pk_name,$options);
		else
			$this->db->order_by($orderby,$options);
			
		$query = $this->db->get($table);
		//trace($this->db);
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}
	}
	
	/**
	 * Returns the list by Select fields's Name (SELECT & ORDER BY,  with Condition)
	 * 
	 * Ex: SELECT distinct(substr(text(FILD_NAME), 0, 5)) AS NAME', ORDER BY substr(text(FILD_NAME), 0, 5) DESC
	 */
	function get_select($table, $selects, $orderby = FALSE, $options = FALSE)
	{	
		$this->db->select_one($selects);
		
		if($orderby == FALSE)
			$this->db->order_by_one($this->pk_name,$options);
		else
			$this->db->order_by_one($orderby,$options);
		
		$query = $this->db->get($table);
//trace($this->db->queries);		
		if(!empty($query))
		{				
			if ( $query->num_rows() > 0 )
			{
				$datas = $query->result_array();
				return $datas;			
			}			
		}
	}
	
	function get_select_fields($table, $wheres, $selects, $orderby = FALSE, $options = FALSE)
	{	
		$this->db->select_one($selects);
		
		foreach($wheres as $key	=> $data)
		{
			if(is_array($data))
				$this->db->where_in($key, $data);
			else
				$this->db->where($key, $data);
		}

		if($orderby != FALSE)		
			$this->db->order_by($orderby, ($options != 'trace') ? $options : FALSE);
		
		$query = $this->db->get($table);
		
		if($options == 'trace')
			trace($this->db->queries);
			
		if(!empty($query))
		{				
			if ( $query->num_rows() > 0 )
			{
				$datas = $query->result_array();
				return $datas;			
			}			
		}
	}
	
	/**
	 * Returns the record by one field
	 *
	 */
	function get_by_sql($sql, $option=false)
	{
		$query	= $this->db->query($sql);
		
		if($option == 'trace')
			trace($this->db->queries);		
			
		if(!empty($query))
		{
			$results = array();
			if ( $query->num_rows() > 0 )
				$results = $query->result_array();
			return $results;	
		}
	}
	
	function count_by_sql($sql)
	{
		$query	= $this->db->query($sql);
		
		if(!empty($query))
		{
			$count = $query->num_rows();
			return $count;	
		}
	}
	
	/**
	 * Returns the record by one field
	 *
	 */
	function get_by_field($table, $field, $value, $orderby = FALSE, $options = FALSE)
	{	
		$this->db->where($field, $value);
		
		if($orderby == FALSE)
			$this->db->order_by($this->pk_name,$options);
		else
			$this->db->order_by($orderby,$options);
		
		$query = $this->db->get($table);
		//trace($this->db->queries);
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}
	}
	
	/**
	 * Returns Select fields's Name by one field 
	 *
	 */
	function get_select_by_fields($table, $datas, $selects, $orderby = FALSE, $options = FALSE, $groupby= FALSE)
	{	
		$this->db->select($selects);
		
		foreach($datas as $key	=> $data)
		{
			if(is_array($data))
				$this->db->where_in($key, $data);
			else
				$this->db->where($key, $data);
		}
		
		if($groupby != FALSE)
			$this->db->group_by($groupby);
		
		if($orderby == FALSE)
			$this->db->order_by($this->pk_name,$options);
		else
			$this->db->order_by($orderby,$options);
			
		$query = $this->db->get($table);
		
		//trace($this->db->queries);		
		
		if(!empty($query))
		{				
			if ( $query->num_rows() > 0 )
			{
				$datas = $query->result_array();
				return $datas;			
			}			
		}
	}
	
	/**
	 * Returns the record by fields
	 *
	 */
	function get_by_fields($table, $datas, $orderby = FALSE, $options = FALSE)
	{	
		foreach($datas as $key	=> $data)
		{
			if(is_array($data))
				$this->db->where_in($key, $data);
			else
				$this->db->where($key, $data);
		}		
		
		if($orderby == FALSE)
			$this->db->order_by($this->pk_name,$options);
		else
			$this->db->order_by($orderby,$options);
		
		$query = $this->db->get($table);
//trace($this->db->queries);
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}	
	}
	
	//------------------------------------------------------------------------------------------
		// check filed_name, datatype on table
	
	/**
	 * Count row in table
	 *
	 */
	function count_rows($table, $posts = FALSE, $date = FALSE,$orderby = FALSE, $options = FALSE, $limit = FALSE, $offset = FALSE)
	{	
		if($posts != FALSE)
		{
			if(is_array($posts))
			{
				$datas = array();
				
				// Get Fields Name & Data Type of Fields
				$fields	= $this->get_datatype(array('table_name' => $table));
				
				foreach($fields as $field)
				{
					if(!empty($posts[$field['column_name']]))
					{	
						if(is_array($posts[$field['column_name']]))
						{
							$this->db->where_in($field['column_name'], $posts[$field['column_name']]);
						}
						else
						{
							$datas[$field['column_name']] = $posts[$field['column_name']];
							
							if($field['data_type'] == 'integer')
								$this->db->where($field['column_name'], $posts[$field['column_name']]);
							else
								$this->db->like('lower('.$field['column_name'].')',strtolower($posts[$field['column_name']]));
						}
					}
				}
			}
			else
				$this->db->where($posts);
		}
			
		if($date != FALSE)
			$this->db->where($date);
		
		if($orderby == FALSE)
			$this->db->order_by($this->pk_name,$options);
		else
			$this->db->order_by($orderby,$options);
		
		if( $limit == FALSE && $offset == FALSE)
			$query = $this->db->get($table);
		else
			$query = $this->db->get($table,$limit,$offset);
			
//trace($this->db->queries);
		if(!empty($query))
		{	
			$count = $query->num_rows();
			return $count;
		}
	}	
	
	/**
	 * Count row1 in table
	 *
	 */
	function count_rows1($table, $posts = FALSE, $date = FALSE,$orderby = FALSE, $options = FALSE, $limit = FALSE, $offset = FALSE)
	{	
		if($posts != FALSE)
		{
			$this->db->where($posts);
		}
			
		if($date != FALSE)
			$this->db->where($date);
		
		if($orderby == FALSE)
			$this->db->order_by($this->pk_name,$options);
		else
			$this->db->order_by($orderby,$options);
		
		if( $limit == FALSE && $offset == FALSE)
			$query = $this->db->get($table);
		else
			$query = $this->db->get($table,$limit,$offset);
			
//trace($this->db->queries);
		if(!empty($query))
		{	
			$count = $query->num_rows();
			return $count;
		}
	}
	
	/**
	 * Get by filter
	 *
	 */
	function get_by_filter($table, $posts = FALSE, $orderby = FALSE, $options = FALSE, $limit = FALSE, $offset = FALSE)
	{	
		if($posts != FALSE)
		{
			$datas = array();
			// Get Fields Name & Data Type of Fields
			$fields	= $this->get_datatype(array('table_name' => $table));
			
			foreach($fields as $field)
			{
				if(!empty($posts[$field['column_name']]))
				{	
					if(is_array($posts[$field['column_name']]))
					{
						$this->db->where_in($field['column_name'], $posts[$field['column_name']]);
					}
					else
					{
						$datas[$field['column_name']] = $posts[$field['column_name']];
						
						if($field['data_type'] == 'integer')
							$this->db->where($field['column_name'], $posts[$field['column_name']]);
						else
							$this->db->like('lower('.$field['column_name'].')',strtolower($posts[$field['column_name']]));
					}
				}
			}
		}
		
		if($orderby == FALSE)
			$this->db->order_by($this->pk_name,$options);
		else
			$this->db->order_by($orderby,$options);
		
		if( $limit == FALSE && $offset == FALSE)
			$query = $this->db->get($table);
		else
			$query = $this->db->get($table,$limit,$offset);
//trace($this->db->queries);
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}	
	}	
	
	//------------------------------------------------------------------------------------------
		// for no need check filed_name, datatype  on table
	
	/**
	 * Count row in table
	 *
	 */
	function count_rows_join_by_filter($table, $selects, $joins, $wheres, $likes = FALSE, $between = FALSE, $groupby = FALSE)
	{			
		$this->db->select($selects);	
		
		foreach($joins as	$key => $join)	
			$this->db->join($key, $join[0], $join[1]);
	
		if($wheres != FALSE)
			$this->db->where($wheres);
		
		if($likes != FALSE)
			$this->db->like($likes);
		
		if($between != FALSE)
			$this->db->where($between);
		
		if($groupby != FALSE)
			$this->db->group_by($groupby);
		
		$query = $this->db->get($table);
//trace($this->db->queries);
		if(!empty($query))
		{	
			$count = $query->num_rows();
			return $count;
		}
	}	
	function count_rows_join_by_filter2($table, $selects, $joins, $wheres, $likes = FALSE, $between = FALSE, $groupby = FALSE)
	{			
		$this->db->select_one($selects);	
		
		foreach($joins as	$key => $join)	
			$this->db->join($key, $join[0], $join[1]);
	
		if($wheres != FALSE)
		{
			foreach($wheres as $key => $value)
			{
				if(is_array($value))
					$this->db->where_in($key, $value);
				else
					$this->db->where($key, $value);
			}
		}
		
		if($likes != FALSE)
			$this->db->like($likes);
		
		if($between != FALSE)
			$this->db->where($between);
		
		if($groupby != FALSE)
			$this->db->group_by($groupby);
		
		$query = $this->db->get($table);
//trace($this->db->queries);
		if(!empty($query))
		{	
			$count = $query->num_rows();
			return $count;
		}
	}
		
	/**
	 * 	Join by filter	
	 *
	 */
	function join_by_filter($table, $selects, $joins, $wheres, $likes = FALSE, $between = FALSE, $orderby = FALSE, $options = FALSE, $groupby = FALSE, $limit = FALSE, $offset = FALSE)
	{	
		$this->db->select($selects);		
		
		foreach($joins as	$key => $join)	
			$this->db->join($key, $join[0], $join[1]);
	
		if($wheres != FALSE)
			$this->db->where($wheres);
		
		if($likes != FALSE)
			$this->db->like($likes);		
			
		if($between != FALSE)
			$this->db->where($between);	
		
		if($orderby == FALSE)
		{
			$this->db->order_by($this->pk_name, $options);
		}
		else
			$this->db->order_by($orderby, ($options != 'trace') ? $options : FALSE);
			
		if($groupby != FALSE)
			$this->db->group_by($groupby);
		
		if( $limit == FALSE && $offset == FALSE)
			$query = $this->db->get($table);
		else
			$query = $this->db->get($table,$limit,$offset);

//trace($this->db->queries);
		if($options == 'trace')
			trace($this->db->queries);
			
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}	
	}
	
	function join_by_filter2($table, $selects, $joins, $wheres, $likes = FALSE, $between = FALSE, $orderby = FALSE, $options = FALSE, $groupby = FALSE, $limit = FALSE, $offset = FALSE)
	{	
		$this->db->select_one($selects);		
		
		foreach($joins as	$key => $join)	
			$this->db->join($key, $join[0], $join[1]);
	
		if($wheres != FALSE)
		{
			foreach($wheres as $key => $value)
			{
				if(is_array($value))
					$this->db->where_in($key, $value);
				else
					$this->db->where($key, $value);
			}
		}
		
		if($likes != FALSE)
			$this->db->like($likes);		
			
		if($between != FALSE)
			$this->db->where($between);	
		
		if($orderby == FALSE)
		{
			$this->db->order_by($this->pk_name, $options);
		}
		else
			$this->db->order_by($orderby, ($options != 'trace') ? $options : FALSE);
			
		if($groupby != FALSE)
			$this->db->group_by($groupby);
		
		if( $limit == FALSE && $offset == FALSE)
			$query = $this->db->get($table);
		else
			$query = $this->db->get($table,$limit,$offset);
		
		if($options == 'trace')
			trace($this->db->queries);
		//trace($this->db->queries);
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}	
	}
	
	//------------------------------------------------------------------------------------------
		
	
	/**
	 * Get Join by filter
	 *
	 */
	function get_join_by_filter($table, $joins, $selects, $posts, $orderby = FALSE, $options = FALSE, $groupby = FALSE, $limit = FALSE, $offset = FALSE)
	{	
		$datas = array();
		// Get Fields Name & Data Type of Fields
		$fields	= $this->get_datatype(array('table_name' => $table));
		
		foreach($fields as $field)
		{			
			if(!empty($posts[$table.'.'.$field['column_name']]))
			{	
				if(is_array($posts[$table.'.'.$field['column_name']]))
				{
					$this->db->where_in($field['column_name'], $posts[$table.'.'.$field['column_name']]);
				}
				else
				{
					$datas[$field['column_name']] = $posts[$table.'.'.$field['column_name']];
					
					if($field['data_type'] == 'integer')
						$this->db->where($table.'.'.$field['column_name'], $posts[$table.'.'.$field['column_name']]);
					else
						$this->db->like('lower('.$table.'.'.$field['column_name'].')',strtolower($posts[$table.'.'.$field['column_name']]));
				}
			}
		}

		$this->db->select($selects);		
		
		foreach($joins as	$key => $join)	
			$this->db->join($key, $join[0], $join[1]);
	
		if($orderby == FALSE)
			$this->db->order_by($this->pk_name,$options);
		else
			$this->db->order_by($orderby,$options);
			
		if($groupby != FALSE)
			$this->db->group_by($groupby);
		
		if( $limit == FALSE && $offset == FALSE)
			$query = $this->db->get($table);
		else
			$query = $this->db->get($table,$limit,$offset);
//trace( $this->db->queries);
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}	
	}
	
	//------------------------------------------------------------------------------------------
		// Check field_name, datatype of table by Select field_name
	
	/**
	 * Count row in table
	 *
	 */
	function count_rows2($table, $joins, $selects, $posts, $orderby = FALSE, $options = FALSE, $groupby = FALSE)
	{		
		$datas = array();
		// Get Fields Name & Data Type of Fields
		$fields	= $this->get_datatype(array('table_name' => $table));
		
		foreach($fields as $field)
		{			
			if(!empty($posts[$table.'.'.$field['column_name']]))
			{	
				if(is_array($posts[$table.'.'.$field['column_name']]))
				{
					$this->db->where_in($field['column_name'], $posts[$table.'.'.$field['column_name']]);
				}
				else
				{
					$datas[$field['column_name']] = $posts[$table.'.'.$field['column_name']];
					
					if($field['data_type'] == 'integer')
						$this->db->where($table.'.'.$field['column_name'], $posts[$table.'.'.$field['column_name']]);
					else
						$this->db->like('lower('.$table.'.'.$field['column_name'].')',strtolower($posts[$table.'.'.$field['column_name']]));
				}
			}
		}

		$this->db->select_one($selects);		
		
		foreach($joins as	$key => $join)	
			$this->db->join($key, $join[0], $join[1]);
	
		if($orderby == FALSE)
			$this->db->order_by($this->pk_name,$options);
		else
			$this->db->order_by($orderby,$options);
			
		if($groupby != FALSE)
			$this->db->group_by($groupby);

		$query = $this->db->get($table);

		if(!empty($query))
		{	
			$count = $query->num_rows();
			return $count;
		}
	}
	
	/**
	 * Get Join by filter2 
	 *
	 */
	function get_join_by_filter2($table, $joins, $selects, $posts, $orderby = FALSE, $options = FALSE, $groupby = FALSE, $limit = FALSE, $offset = FALSE)
	{	
		$datas = array();
		// Get Fields Name & Data Type of Fields
		$fields	= $this->get_datatype(array('table_name' => $table));
		
		foreach($fields as $field)
		{			
			if(!empty($posts[$table.'.'.$field['column_name']]))
			{	
				if(is_array($posts[$table.'.'.$field['column_name']]))
				{
					$this->db->where_in($field['column_name'], $posts[$table.'.'.$field['column_name']]);
				}
				else
				{
					$datas[$field['column_name']] = $posts[$table.'.'.$field['column_name']];
					
					if($field['data_type'] == 'integer')
						$this->db->where($table.'.'.$field['column_name'], $posts[$table.'.'.$field['column_name']]);
					else
						$this->db->like('lower('.$table.'.'.$field['column_name'].')',strtolower($posts[$table.'.'.$field['column_name']]));
				}
			}
		}

		$this->db->select_one($selects);		
		
		foreach($joins as	$key => $join)	
			$this->db->join($key, $join[0], $join[1]);
	
		if($orderby == FALSE)
			$this->db->order_by($this->pk_name,$options);
		else
			$this->db->order_by($orderby,$options);
			
		if($groupby != FALSE)
			$this->db->group_by($groupby);
		
		if( $limit == FALSE && $offset == FALSE)
			$query = $this->db->get($table);
		else
			$query = $this->db->get($table,$limit,$offset);
//trace( $this->db->queries);
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}	
	}
	
	/**
	 * get max by fields
	 *
	 */	
	function get_max_by_fields($table, $posts = FALSE, $select)
	{	
		if($posts != FALSE)
		{
			$fields	= $this->get_datatype(array('table_name' => $table));
		
			foreach($fields as $field)
			{
				if(!empty($posts[$field['column_name']]))
				{
					if(is_array($posts[$field['column_name']]))
					{
						$this->db->where_in($field['column_name'], $posts[$field['column_name']]);
					}
					else
					{
						$datas[$field['column_name']] = $posts[$field['column_name']];
						
						if($field['data_type'] == 'integer')
							$this->db->where($field['column_name'], $posts[$field['column_name']]);
						else
							$this->db->like('lower('.$field['column_name'].')',strtolower($posts[$field['column_name']]));
					}
				}
			}
		}
		
		$this->db->select_max($select);		
		$query = $this->db->get($table);
		
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->row($select);
			return $data;
		}	
	}
	
	/**
	 * get min by fields
	 *
	 */	
	function get_min_by_fields($table, $field,$posts = FALSE)
	{	
		if($posts != FALSE)
		{
			$this->db->where($posts);
		}
		
		$this->db->select_min($field);		
		$query = $this->db->get($table);
		//trace($this->db->queries);
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->row($field);
			return $data;
		}	
	}
	
	/**
	 * get sum by fields
	 *
	 */	
	function get_sum_by_fields($table, $posts = FALSE, $select)
	{	
		if($posts != FALSE)
		{
			$fields	= $this->get_datatype(array('table_name' => $table));
		
			foreach($fields as $field)
			{
				if(!empty($posts[$field['column_name']]))
				{
					if(is_array($posts[$field['column_name']]))
					{
						$this->db->where_in($field['column_name'], $posts[$field['column_name']]);
					}
					else
					{
						$datas[$field['column_name']] = $posts[$field['column_name']];
						
						if($field['data_type'] == 'integer')
							$this->db->where($field['column_name'], $posts[$field['column_name']]);
						else
							$this->db->like('lower('.$field['column_name'].')',strtolower($posts[$field['column_name']]));
					}
				}
			}
		}
		
		$this->db->select_sum($select);		
		$query = $this->db->get($table);

		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->row($select);
			return $data;
		}	
	}

	// ------------------------------------------------------------------------	
	
	/**
	 * Check Duplicate
	 *
	 */ 	 
	function duplicate($table, $wheres, $likes=false, $option=false)
	{	
		if($likes != false)
			$this->db->like($likes);
			
		$query = $this->db->get_where($table, $wheres);
		
		if($option == 'trace')
			trace($this->db->queries);
			
		if(!empty($query)):
			if ( $query->num_rows() > 0 )
				return true;
			else
				return false;
		endif;
		
		return false;
	}
	
	// ------------------------------------------------------------------------	
	
	/**
	 * Get Data Type of Fileds
	 *
	 */ 	 
	function get_datatype($data)
	{	
		$this->db->select('column_name,data_type');		
		$query = $this->db->get_where('information_schema.columns', $data);
		
		if(!empty($query))
		{	
			$results = array();
			
			if ( $query->num_rows() > 0 )
				$results = $query->result_array();
			
			return $results;
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Insert
	 *
	 */	
	function insert($table, $data, $option = false)
	{
		$affected_rows	= $this->db->insert($table, $data);
		
		if($option == 'trace')
			trace($this->db->queries);
			
		return $affected_rows;
	}

	// ------------------------------------------------------------------------

	
	/**
	 * Update
	 *
	 */	
	function update($table, $data, $option = false)
	{
		$this->db->where($this->pk_name, $data[$this->pk_name]);
		$affected_rows	= $this->db->update($table, $data);		
		
		if($option == 'trace')
			trace($this->db->queries);
		
		return $affected_rows;
	}	
	
	/**
	 * Update
	 *
	 */	
	function update_field($table, $data, $where, $option = false)
	{
		$affected_rows	= $this->db->update($table,$data,$where);		
		
		if($option == 'trace')
			trace($this->db->queries);
			
		return $affected_rows;
	}	
	// ------------------------------------------------------------------------

	
	/**
	 * Delete
	 *
	 */	
	function delete($table, $data)
	{
		$affected_rows	= $this->db->delete($table, $data);
		return $affected_rows;
	}
	/**
	 * select max(field)
	 *
	 */	
	function select_max_field($table, $field_name, $posts = false)
	{
		
		$this->db->select_max($field_name);
		if($posts != false){		
			foreach($posts as $key => $p){
				if(is_numeric($p))
					$this->db->where($key, $p);
				else
					$this->db->like('lower('.$key.')',strtolower($p));
			}
		}//end if 
		
		$query = $this->db->get($table);
		//trace($query);
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}	
	}
	
	/**
	 * select sum(field)
	 *
	 */	
	function select_sum_field($table, $field_name, $posts = false)
	{
		
		$this->db->select_sum($field_name);
		if($posts != false){		
			foreach($posts as $key => $p){
				if(is_numeric($p))
					$this->db->where($key, $p);
				else
					$this->db->like('lower('.$key.')',strtolower($p));
			}
		}//end if 
		
		$query = $this->db->get($table);
		//trace($this->db->queries);		
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}	
	}
	/**
	 * Count All Results
	 *
	 */	
	function count_all_results($table, $posts = false)
	{	
		if($posts	!= false)
			$this->db->where($posts);
	
		$this->db->from($table);
		$data	= $this->db->count_all_results();
		
		return $data;
	}
	
	/**
	 * select sum(field)
	 *
	 */	
	function select_distinct_by_fields($table, $wheres = FALSE, $select)
	{
		$this->db->distinct();
		$this->db->select($select);
		
		if($wheres != FALSE)
		{
			foreach($wheres as $key => $value)
			{
				if(is_array($value))
					$this->db->where_in($key, $value);
				else
					$this->db->where($key, $value);
			}
		}
		
		$query = $this->db->get($table);
		
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}	
	}
	
	/**
	 * Truncate Table
	 *
	 *
	 */
	function truncate($table)
	{		
		$this->db->truncate($table);
	}
	
		
} // End Class
/* End of file sibilling_model.php */
/* Location: ./application/models/sibilling_model.php */